package com.andy.process.source;

import com.andy.common.entity.Configuration;
import com.andy.common.entity.Connect;
import com.andy.common.entity.Bean;
import com.andy.common.entity.Schema;
import com.andy.common.exception.CastParseException;
import com.andy.common.parser.YamlFactory;
import com.andy.common.parser.impl.ExcelFileParser;
import com.andy.process.abstracts.AbstractSource;
import com.andy.process.interfaces.impl.ExcelDataParser;
import com.andy.process.threadpool.Connector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.FutureTask;

/**
 * Created by Administrator on 2017/8/31.
 */
public class ExcelSource extends AbstractSource {
    private final static Logger LOG = LoggerFactory.getLogger(ExcelSource.class);
    private YamlFactory yamlFactory;
    private ExcelDataParser dataParser;
    private Configuration conf;
    private final int eachSize = 500;
    private Connection connection;


    /**
     * construct ExcelParser and get configuration
     *
     * @param clazz
     * @param path
     */
    public ExcelSource(Class clazz, String path) {
        if (yamlFactory == null) {
            yamlFactory = new ExcelFileParser(clazz, path);
        }
        try {
            conf = (Configuration) yamlFactory.getObj();
            connection = getConnection(conf);
        } catch (CastParseException e) {
            LOG.error("cast parser error {}", e);
        }
    }

    /**
     * insert data into mysql
     */
    public void insert() throws SQLException {
        List<Bean> beans = getDataSource();
        List<Bean> copyBeans = beans;
        if(beans.size()<=0)
            return;
        Bean beanOne = beans.get(0);
        String[] names = beanOne.getName();
        int num = beans.size() / eachSize;
        for (int i = 0; i < num ; i++) {
            StringBuffer sql = new StringBuffer();
            sql.append("insert into paipai(");
            for (int x = 0; x < names.length; x++) {
                sql.append(names[x] + ",");
            }
            sql = sql.deleteCharAt(sql.lastIndexOf(","));
            sql.append(")values");
            beans = copyBeans.subList(i * eachSize, (i + 1) * eachSize);
            StringBuffer value = new StringBuffer();
            for (Bean bean : beans) {
                Object[] values = bean.getValue();
                value.append("(");
                for (int j = 0; j < values.length; j++) {
                    value.append("'" + values[j].toString() + "',");
                }
                value = value.deleteCharAt(value.lastIndexOf(","));
                value.append("),");
            }
            value = value.deleteCharAt(value.lastIndexOf(","));
            sql.append(value);
            PreparedStatement pStmt = connection.prepareStatement(sql.toString());
            pStmt.execute();
        }
    }

    /**
     * get excel data
     */
    @Override
    public List<Bean> getDataSource() {
        Schema schema = conf.getSchemaByName(conf.getTargetSchema());
        dataParser = new ExcelDataParser(conf.getSourcePath(), schema);
        List<Bean> beans = dataParser.parser(conf);
        return beans;
    }
}
